package com.vladimirkolarevic.eventplanner.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Junction;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;



import com.vladimirkolarevic.eventplanner.Attend;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.AttendBean;
import com.vladimirkolarevic.eventplanner.beans.UserBean;
import com.vladimirkolarevic.eventplanner.service.AttendService;

public class AttendServiceHibernate implements AttendService {

	private final Session session;
	
	public AttendServiceHibernate(Session session) {
		this.session = session;
	}
	
	
	public List<Attend> retrive() {
		List<Attend> attends = session.createCriteria(AttendBean.class).list();
		return null;
	}


	public Attend retrive(Long id) {
		
		return (Attend) session.get(AttendBean.class, id);
	}

	public void attach(Attend a){
		session.lock(a, LockMode.NONE);
	}
	
	
	public Long save(Attend attend) {
		// TODO Auto-generated method stub
		return (Long) session.save(attend);
	}

	
	public Attend retrive(User user) {
		/*Example userExample = Example.create(attend)
		.excludeZeroes()
		.enableLike(MatchMode.ANYWHERE).ignoreCase();*/
		Criteria crit = session.createCriteria(AttendBean.class).createCriteria("user").add(Restrictions.like("id", user.getId()));
		return (Attend) crit.uniqueResult();
	}

	
	public void update(Attend attend) {
		session.update(attend);		
	}

}
